<?php
/* 
 * ebiz front
 * 
 * CategoryProductDao Model
 * 
 * @author allen
 * 
 */
require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class CategoryProductDao extends BaseDao 
{
	
    private $tableName = "category_products";
     
    public function __construct($dbUtil){ 
        parent::__construct($dbUtil);
    }
    
    public function getByCategoryId($categoryId){   //not used
        $param = array($categoryId);
        $sql = "select * from $this->tableName where category_id = ? and is_delete = 0";
        return $this->dbUtil->SelectData($sql, $param);
    }
    
    public function getByProductId($productId){  
        $param = array($productId);
        $sql = "select cp.*, c.parent_id , c.url_title as categoryUrlTitle , c.short_name from $this->tableName as cp left join categories as c on cp.category_id = c.id where cp.product_id = ? and cp.is_delete = 0";
        $arr_result = $this->dbUtil->SelectData($sql, $param);
        if(empty($arr_result))
            return array();
        $arr_sort = array();
        $int_parentid = 0;
        while( $arr_child = $this->getChildCategory($int_parentid,$arr_result) ){
            $arr_sort[] = $arr_child;
        	$int_parentid = $arr_child['category_id'];
        }
        return $arr_sort;
    }
    
    protected function getChildCategory($int_parentid, $arr_categories){
    	foreach($arr_categories as $arr_category){
    		if($int_parentid == $arr_category['parent_id'])
    		  return $arr_category;
    	}
    	return false;
    }
    
    public function getByAllCategoryIds($categoryIds){      //through cids to find a product item info
    	$categoryIds = mysql_escape_string($categoryIds);
    	$param = array();
        $sql = "select * from $this->tableName where category_id in ($categoryIds) and is_delete = 0 ";
        return $this->dbUtil->SelectData($sql, $param);
    	
    }
    
}



